package mapEditor;

import debug.*;

import mapEditor.utils.SvgFont;
import mapEditor.utils.SvgFill;
import mapEditor.utils.SvgStroke;
import mapEditor.utils.Util;

import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.Properties;

public class EditorProperties
{ 
  private static final String WORKSPACE_DIRECTORY="WorkSpaceDirectory";
  private static final String FONT_FAMILY="Font.Family";
  private static final String FONT_STYLE="Font.Style";
  private static final String FONT_WEIGHT="Font.Weight";
  private static final String FONT_SIZE="Font.Size"; //
  private static final String FILL_COLOR="Fill.Color";
  private static final String FILL_OPACITY="Fill.Opacity";
  private static final String FILL_RULE="Fill.Rule";
  private static final String STROKE_COLOR="Stroke.Color";
  private static final String STROKE_DASHARRAY="Stroke.Dasharray";
  private static final String STROKE_DASHOFFSET="Stroke.Dashoffset";
  private static final String STROKE_LINECAP="Stroke.Linecap";
  private static final String STROKE_LINEJOIN="Stroke.Linejoin";
  private static final String STROKE_MITERLIMIT="Stroke.Miterlimit";
  private static final String STROKE_OPACITY="Stroke.Opacity";
  private static final String STROKE_WIDTH="Stroke.Width";
	
  private static final String defaultWorkSpace="/Application Data/MapEditor";
  private static Properties properties;
  private static boolean opened=false;
  
  static //
  { properties=new Properties();
    setDefaults();
    load(); // try read Property File
    opened=true;
  }

  public static String getUserName()
  { return(System.getProperty("user.name")); }

  public static String getUserHomeDirectory()
  { return(System.getProperty("user.home")); }

  public static String getWorkSpaceDirectory()
  { return(properties.getProperty(WORKSPACE_DIRECTORY)); }

  public static void setWorkSpaceDirectory(String workSpaceDirectory)
  { properties.setProperty(WORKSPACE_DIRECTORY,workSpaceDirectory);
    if(opened) store();
  }
  
  public static void loadFont(SvgFont font)
  {	font.setFontFamily(properties.getProperty(FONT_FAMILY));
	font.setFontStyle(SvgFont.fontStyle(properties.getProperty(FONT_STYLE)));
	font.setFontWeight(properties.getProperty(FONT_WEIGHT));
	font.setFontSize(Integer.parseInt(properties.getProperty(FONT_SIZE)));
  }
  
  public static void storeFont(SvgFont font)
  {	properties.setProperty(FONT_FAMILY,font.getFontFamily());
	properties.setProperty(FONT_STYLE,SvgFont.fontStyle(font.getFontStyle()));
	properties.setProperty(FONT_WEIGHT,font.getFontWeight());
	properties.setProperty(FONT_SIZE,""+font.getFontSize());
    store();
  }
  
  public static void loadFill(SvgFill fill)
  { fill.setColor(Util.decodeRGB(properties.getProperty(FILL_COLOR)));
    fill.setOpacity(Float.parseFloat(properties.getProperty(FILL_OPACITY)));
    fill.setRule(Util.fillRule(properties.getProperty(FILL_RULE)));
  }
  
  public static void storeFill(SvgFill fill)
  { properties.setProperty(FILL_COLOR,Util.encodeRGB(fill.getColor()));
	properties.setProperty(FILL_OPACITY,""+fill.getOpacity());
	properties.setProperty(FILL_RULE,Util.fillRule(fill.getRule()));
    store();
  }
  
  public static void loadStroke(SvgStroke stroke)
  { stroke.setColor(Util.decodeRGB(properties.getProperty(STROKE_COLOR)));
    stroke.setDashArray(Util.decodeFloats(properties.getProperty(STROKE_DASHARRAY),"none"));
    stroke.setDashOffset(Float.parseFloat(properties.getProperty(STROKE_DASHOFFSET)));
    stroke.setLineCap(Util.lineCap(properties.getProperty(STROKE_LINECAP)));
    stroke.setLineJoin(Util.lineJoin(properties.getProperty(STROKE_LINEJOIN)));
    stroke.setMiterLimit(Float.parseFloat(properties.getProperty(STROKE_MITERLIMIT)));
    stroke.setOpacity(Float.parseFloat(properties.getProperty(STROKE_OPACITY)));
    stroke.setStrokeWidth(Float.parseFloat(properties.getProperty(STROKE_WIDTH)));
  }
  
  public static void storeStroke(SvgStroke stroke)
  { properties.setProperty(STROKE_COLOR,Util.encodeRGB(stroke.getColor()));
	properties.setProperty(STROKE_DASHARRAY,Util.encodeFloats(stroke.getDashArray(),"none"));
	properties.setProperty(STROKE_DASHOFFSET,""+stroke.getDashOffset());
	properties.setProperty(STROKE_LINECAP,Util.lineCap(stroke.getLineCap()));
	properties.setProperty(STROKE_LINEJOIN,Util.lineJoin(stroke.getLineJoin()));
	properties.setProperty(STROKE_MITERLIMIT,""+stroke.getMiterLimit());
	properties.setProperty(STROKE_OPACITY,""+stroke.getOpacity());
	properties.setProperty(STROKE_WIDTH,""+stroke.getStrokeWidth());
    store();
  }
  
  
  private static void setDefaults()
  { setWorkSpaceDirectory(getUserHomeDirectory()+defaultWorkSpace);  
    properties.setProperty(FONT_FAMILY,SvgFont.DEFAULT_FONT_FAMILY);
	properties.setProperty(FONT_STYLE,SvgFont.fontStyle(SvgFont.DEFAULT_FONT_STYLE));
	properties.setProperty(FONT_WEIGHT,SvgFont.DEFAULT_FONT_WEIGHT);
	properties.setProperty(FONT_SIZE,""+SvgFont.DEFAULT_FONT_SIZE);
	properties.setProperty(FILL_COLOR,Util.encodeRGB(SvgFill.DEFAULT_COLOR));
	properties.setProperty(FILL_OPACITY,""+SvgFill.DEFAULT_OPACITY);
	properties.setProperty(FILL_RULE,Util.fillRule(SvgFill.DEFAULT_RULE));
    properties.setProperty(STROKE_COLOR,Util.encodeRGB(SvgStroke.DEFAULT_COLOR));
	properties.setProperty(STROKE_DASHARRAY,Util.encodeFloats(SvgStroke.DEFAULT_DASH_ARRAY,"none"));
	properties.setProperty(STROKE_DASHOFFSET,""+SvgStroke.DEFAULT_DASH_OFFSET);
	properties.setProperty(STROKE_LINECAP,Util.lineCap(SvgStroke.DEFAULT_LINE_CAP));
	properties.setProperty(STROKE_LINEJOIN,Util.lineJoin(SvgStroke.DEFAULT_LINE_JOIN));
	properties.setProperty(STROKE_MITERLIMIT,""+SvgStroke.DEFAULT_MITER_LIMIT);
	properties.setProperty(STROKE_OPACITY,""+SvgStroke.DEFAULT_OPACITY);
	properties.setProperty(STROKE_WIDTH,""+SvgStroke.DEFAULT_WIDTH);
  }
  
  private static void load()
  { try
    { File dir=new File(getWorkSpaceDirectory()+"/MetaData");
      File file=new File(dir,"EditorProperties.txt");
      Debug.println("EditorProperties.load: file="+file.getAbsolutePath()+", Name: "+file.getName()+".");
      dir.mkdirs();
      InputStream inputStream=new BufferedInputStream(new FileInputStream(file));
      properties.loadFromXML(inputStream);
      inputStream.close();
      properties.list(System.out);
    } catch(IOException e) { Debug.EXCEPTION("EditorProperties.load",e); }
  }
  
  private static void store()
  { try
    { File dir=new File(getWorkSpaceDirectory()+"/MetaData");
      File file=new File(dir,"EditorProperties.txt");
      Debug.println("EditorProperties.store: file="+file.getAbsolutePath()+", Name: "+file.getName()+".");
      properties.list(System.out);
      dir.mkdirs();
      OutputStream outputStream=new BufferedOutputStream(new FileOutputStream(file));
      properties.storeToXML(outputStream,"MapEditor general properties");
      outputStream.close();
    } catch(IOException e) { Debug.EXCEPTION("EditorProperties.store",e); }
  }
}
